<template>
  <div>
    <el-card>
      <el-table style="margin: 10px 0px" :data="skuArr">
        <el-table-column
          label="序号"
          type="index"
          algin="center"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="名称"
          show-overflow-tooltip
          width="150px"
          prop="skuName"
        ></el-table-column>
        <el-table-column
          label="描述"
          width="150px"
          prop="skuDesc"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="图片" width="150px">
          <template #="{ row}">
            <img :src="row.skuDefaultImg" alt="" style="width: 90px; height: 90px" />
          </template>
        </el-table-column>
        <el-table-column label="重量" width="150px" prop="weight"></el-table-column>
        <el-table-column label="价格" width="150px" prop="price"></el-table-column>
        <el-table-column label="操作" width="250px" fixed="right">
          <template #="{ row}">
            <el-button
              type="primary"
              size="default"
              @click="updataSale(row)"
              :icon="row.isSale == 1 ? 'bottom' : 'top'"
            ></el-button>
            <el-button
              type="primary"
              size="default"
              @click="updataSku"
              icon="edit"
            ></el-button>
            <el-button
              type="primary"
              size="default"
              @click="findSku(row)"
              icon="InfoFilled"
            ></el-button>
            <el-popconfirm :title="`确定删除${row.skuName}`" style="width: 200px;" @confirm="deleteSku(row)">
              <template #reference>
                <el-button
                  type="primary"
                  size="default"
                  icon="delete"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize4"
        :page-sizes="[5, 10, 15, 20]"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total,"
        :total="total"
        @current-change="getHasSpu"
        @size-change="handler"
      />
      <!-- 抽屉组件 -->
      <el-drawer v-model="drawer" size="50%">
        <!-- 标题部分 -->
        <template #header>
          <h4>查看商品详情</h4>
        </template>
        <template #default class="rowmargin">
          <el-row style="margin: 30px 0px">
            <el-col :span="6">名称</el-col>
            <el-col :span="18">{{ skuInfo.skuName }}</el-col>
          </el-row>
          <el-row style="margin: 30px 0px">
            <el-col :span="6">描述</el-col>
            <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
          </el-row>
          <el-row style="margin: 30px 0px">
            <el-col :span="6">价格</el-col>
            <el-col :span="18">{{ skuInfo.price }}</el-col>
          </el-row>
          <el-row style="margin: 30px 0px">
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                v-for="(item) in skuInfo.skuAttrValueList"
                :key="item.id"
                label=""
                style="margin: 5px 5px"
                >{{ item.valueName }}</el-tag
              >
            </el-col>
          </el-row>
          <el-row style="margin: 30px 0px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                v-for="(item) in skuInfo.skuSaleAttrValueList"
                :key="item.id"
                style="margin: 5px 5px"
                >{{ item.saleAttrValueName }}</el-tag
              >
            </el-col>
          </el-row>
          <el-row style="margin: 30px 0px">
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <el-carousel :interval="4000" type="card" height="200px">
                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item">
                  <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%" />
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </template>
      </el-drawer>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import {
  reqCancelSale,
  reqOnSale,
  reqRemoveSku,
  reqSkuInfo,
  reqSkuList,
} from "@/api/product/sku";
import { SkuResponseDate, SkuData, SkuInfoData } from "@/api/product/sku/type";
import { ElMessage } from "element-plus";
let pageNo = ref<number>(1); //分页器当前页码
let pageSize4 = ref<number>(5); //分页多少条数据
//
let total = ref<number>(0);
let skuArr = ref<SkuData[]>([]);
//控制抽屉显示与隐藏
let drawer = ref<boolean>(false);
//存储sku详情
let skuInfo = ref<any>({});

//组件载完毕
onMounted(() => {
  getHasSpu();
});
//获取SKU数据方法
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager;
  let result: SkuResponseDate = await reqSkuList(pageNo.value, pageSize4.value);
  if (result.code == 200) {
    total.value = result.data.total;
    skuArr.value = result.data.records;
  }
};
//分页器下拉菜单发生变化
const handler = () => {
  getHasSpu();
};
//商品上架与下架
const updataSale = (row: SkuData) => {
  //如果当前商品的isSale==1,说明商品处于上架状态，需要变更下架
  //如果isSale==0,说明商品处于下架状态，需要变更上架
  if (row.isSale == 1) {
    reqCancelSale(row.id as number);
    ElMessage({
      type: "success",
      message: "下架成功",
    });
    getHasSpu(pageNo.value);
  } else {
    reqOnSale(row.id as number);
    ElMessage({
      type: "success",
      message: "上架成功",
    });
    getHasSpu(pageNo.value);
  }
};
//更新已有的SKU
const updataSku = () => {
  ElMessage({
    type: "success",
    message: "程序员在努力更新中...",
  });
};
//查看商品详情按钮的回调
const findSku = async (row: SkuData) => {
  drawer.value = true;
  let result: SkuInfoData = await reqSkuInfo(row.id as number);
  skuInfo.value = result.data;
};
//
const deleteSku = async (row: SkuData) => {
  let result: any = await reqRemoveSku(row.id as number);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    getHasSpu(skuArr.value.length>1?pageNo.value:pageNo.value-1)
  } else {
    ElMessage({
      type: "error",
      message: "删除失败",
    });
  }
};
</script>

<style scoped>
.rowmargin .el-row {
  margin: 10px 0px;
}
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
